package com.ztstkj.lzservices.mmkvs

import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.DeviceUtils
import com.google.gson.Gson
import com.sscl.baselibrary.utils.DebugUtil
import com.tencent.mmkv.MMKV
import com.ztstkj.lzservices.dto.CardQrcodeOutput
import com.ztstkj.lzservices.dto.DeliveryInput
import com.ztstkj.lzservices.dto.EquipmentOutput
import com.ztstkj.lzservices.dto.LogInput
import com.ztstkj.lzservices.model.SettlementEnum
import com.ztstkj.lzservices.model.SysSetting
import com.ztstkj.lzservices.model.SysTypeEnum

class MmkvManager {
    companion object {
        private const val SYSSETTING_KEY: String = "SysSettingKey"
        private const val EQUIPMENT_KEY: String="EquipmentKey"
        private const val DELIVERY_KEY: String="DeliveryKey"
        private const val LOG_KEY: String="LogKey"
        private const val CARD_QRCODE_KEY:String="Card_Qrcode_Key"
        var enableLog: Boolean=false
        fun getSysSetting(): SysSetting {
            val mmkv = MMKV.defaultMMKV()
            if (mmkv.allKeys()?.contains(SYSSETTING_KEY)==false) {
                return SysSetting(SysTypeEnum.COLLECT.value, SettlementEnum.POINT.value,1000)
            }
            return Gson().fromJson(mmkv.decodeString(SYSSETTING_KEY), SysSetting::class.java)
        }

        fun setSysSetting(sysSetting: SysSetting?) {
            val mmkv = MMKV.defaultMMKV()
            if(sysSetting==null) {
                mmkv.remove(SYSSETTING_KEY)
            }else{
                mmkv.encode(SYSSETTING_KEY, Gson().toJson(sysSetting))
            }
        }

        fun getEquipment(): EquipmentOutput?{
            val mmkv = MMKV.defaultMMKV()
            if (mmkv.allKeys()?.contains(EQUIPMENT_KEY)==false) return null
            return Gson().fromJson(mmkv.decodeString(EQUIPMENT_KEY), EquipmentOutput::class.java)
        }

        fun setEquipment(equipment: EquipmentOutput?) {
            if(equipment==null) return
            val mmkv = MMKV.defaultMMKV()
            mmkv.encode(EQUIPMENT_KEY,Gson().toJson(equipment))
        }

        fun setDelivery(deliveryInput: DeliveryInput?){
            if(deliveryInput==null) return
            val mmkv = MMKV.mmkvWithID(DELIVERY_KEY)
            mmkv.encode(deliveryInput.createTime.toString(),Gson().toJson(deliveryInput))
        }
        fun getLastDelivery(): DeliveryInput?{
            val mmkv = MMKV.mmkvWithID(DELIVERY_KEY)
            if(mmkv.allKeys()?.size==0) return null
            val keys=mmkv.allKeys()?.sorted()
            return Gson().fromJson(mmkv.decodeString(keys?.get(0)),DeliveryInput::class.java)
        }
        fun getDeliveryCount():Int{
            val mmkv = MMKV.mmkvWithID(DELIVERY_KEY)
            return mmkv.allKeys()?.size?:0
        }
        fun getDeliveryImages():List<String>{
            val mmkv = MMKV.mmkvWithID(DELIVERY_KEY)
            if(mmkv.allKeys()?.size==0) return emptyList()
            val keys=mmkv.allKeys()
            val list= mutableListOf<DeliveryInput>()
            for(key in keys!!){
                list.add(Gson().fromJson(mmkv.decodeString(key),DeliveryInput::class.java))
            }
            return list.map { a->a.trashCanPhotos }
        }
        fun removeDelivery(key: String){
            val mmkv = MMKV.mmkvWithID(DELIVERY_KEY)
            mmkv.remove(key)
        }
        private fun addLog(log: LogInput?){
            if(!enableLog) return
            if(log==null) return
            val mmkv = MMKV.mmkvWithID(LOG_KEY)
            mmkv.encode(log.createtime.toString(),Gson().toJson(log))
        }
        fun addLog(title: String,outParam: String){
            DebugUtil.debugOut(title+"\t"+outParam)
            if(!enableLog) return
            val log=LogInput(title,outParam)
            log.createtime= System.currentTimeMillis()/1000
            log.version= AppUtils.getAppVersionName()
            log.realno= DeviceUtils.getAndroidID()

            addLog(log)
        }
        fun getTopLog(): List<LogInput>?{
            if(!enableLog) return null
            val mmkv = MMKV.mmkvWithID(LOG_KEY)
            if(mmkv.allKeys()?.size==0) return null
            val keys=mmkv.allKeys()?.sorted()
            val topKeys=keys?.take(5)
            val result:MutableList<LogInput> = ArrayList()
            for (key in topKeys!!){
                result.add(Gson().fromJson(mmkv.decodeString(key),LogInput::class.java))
            }
            return result
        }
        fun removeLog(keys: List<String>){
            val mmkv = MMKV.mmkvWithID(LOG_KEY)
            for (key in keys) {
                mmkv.remove(key)
            }
        }
        fun removeAll(){
            val mmkv = MMKV.mmkvWithID(LOG_KEY)
            mmkv.clearAll()
        }
        fun getCardQrocodeCount(): Int{
            val mmkv = MMKV.mmkvWithID(CARD_QRCODE_KEY)
            return mmkv.allKeys()?.size ?: 0
        }

        fun setCardQrcodes(list: List<CardQrcodeOutput>?) {
            if(list==null) return
            val mmkv = MMKV.mmkvWithID(CARD_QRCODE_KEY)
            mmkv.clearAll()
            DebugUtil.warnOut("全部清除，${mmkv.allKeys()?.size}")
            list.forEach { item->
                mmkv.encode(item.cardNo,Gson().toJson(item))
            }
            DebugUtil.warnOut("新数据，${mmkv.allKeys()?.size}")
        }
        fun setCardQrcode(input: CardQrcodeOutput) {
            val mmkv = MMKV.mmkvWithID(CARD_QRCODE_KEY)
            mmkv.encode(input.cardNo,Gson().toJson(input))
        }
        fun getCardQrcode(cardNo:String?):CardQrcodeOutput?{
            if(cardNo.isNullOrEmpty()) return null
            val mmkv = MMKV.mmkvWithID(CARD_QRCODE_KEY)
            val jsonStr=mmkv.decodeString(cardNo)
            if(jsonStr.isNullOrEmpty()) return null
            return Gson().fromJson(jsonStr, CardQrcodeOutput::class.java)
        }

    }
}